<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/3/20
 * Time: 11:22
 */

namespace Home\Controller;


class SearchController  extends NavController{
    public function cat_search(){
        $catId = I('get.cat_id');
        //取出商品和翻页
        $goodsModel = D('Admin/Goods');
        $data = $goodsModel->cat_search($catId);
        $goods3 = $goodsModel->getRecGoods('is_best');
        $goods4 = $goodsModel->getRecGoods('is_new');
        //根据上面搜索出来的商品计算筛选条件
        $catModel = D('Admin/Category');
        $searchFilter = $catModel->getSearchConditionByGoodsId($data['goods_id']);
        $this->assign(array(
            'goods4' => $goods4,
            'goods3' => $goods3,
            'page' => $data['page'],
            'data' => $data['data'],
            'searchFilter' => $searchFilter,
            '_page_title' => '分类搜索页',
            '_page_keywords' => '分类搜索页',
            '_page_description' => '分类搜索页',
        ));
        $this->display();
    }

    public function key_search(){
        $key = I('get.key');
        //取出商品和翻页
        $goodsModel = D('Admin/Goods');
        $data = $goodsModel->key_search($key);
        //根据上面搜索出来的商品计算筛选条件
        $catModel = D('Admin/Category');
        $searchFilter = $catModel->getSearchConditionByGoodsId($data['goods_id']);
        $this->assign(array(
            'page' => $data['page'],
            'data' => $data['data'],
            'searchFilter' => $searchFilter,
            '_page_title' => '分类搜索页',
            '_page_keywords' => '分类搜索页',
            '_page_description' => '分类搜索页',
        ));
        $this->display();
    }

    /**处理浏览历史**/
    public function displayHistory(){
        $id = I('get.id');
        //先从COOKIE中取出浏览历史的ID数组
        $data = isset($_COOKIE['display_history']) ? unserialize($_COOKIE['display_history']) : array();
        //把最新浏览的商品放在数组第一位
        array_unshift($data,$id);
        $data = array_unique($data);
        //只取6个
        if(count($data) > 6){
            $data = array_slice($data,0,6);
        }
        //数据存回COOKIE
        setcookie('display_history',serialize($data),time() + 30 * 86400,'/');
        $goodsModel = D('Admin/Goods');
        array_filter($data);
        $data = implode(',',$data);
        $gData = $goodsModel->field('id,mid_logo,goods_name')->where(array(
            'id' => array('in',$data),
            'is_onsale' => array('eq',1),
        ))->order("FIELD(id,$data)")->select();
        echo json_encode($gData);
    }

}